package com.iocup.keybastion.authentication.userdetails.service;



import com.iocup.keybastion.authentication.userdetails.UserDetails;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Description: 用户信息的默认实现
 * @Author: xyjxust
 * @Date: 2021/11/23 15:54
 */
@Setter
@Getter
@NoArgsConstructor
public class DefaultUser implements UserDetails {
    /**
     * 密码
     */
    private String password;
    /**
     * 登录名
     */
    private String loginName;
    /**
     * 资源信息
     */
    private Set<String> resources;
    /**
     * 角色信息
     */
    private Set<String> roles;
    /**
     * 账号是否已经过期 true为未过期
     */
    private boolean accountNonExpired;
    /**
     * 账号是否已经被锁定 true表示没有锁定
     */
    private boolean accountNonLocked;
    /**
     * 凭证是否过期 true 表示没有过期
     */
    private boolean credentialsNonExpired;
    /**
     * 是否禁用 true 表示启用
     */
    private boolean enabled;
    /**
     * id
     */
    private Object id;
    /**
     * 用户姓名
     */
    private String userName;
    /**
     * 缓存其他信息
     */
    private  Map<String, Object> other = new HashMap<>();

    public static DefaultUser build() {
        return new DefaultUser();
    }

    public <T> DefaultUser id(T id) {
        this.id = id;
        return this;
    }

    public DefaultUser accountNonExpired(boolean accountNonExpired) {
        this.accountNonExpired = accountNonExpired;
        return this;
    }

    public DefaultUser credentialsNonExpired(boolean credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
        return this;
    }
    public DefaultUser accountNonLocked(boolean accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
        return this;
    }

    public DefaultUser enabled(boolean enabled) {
        this.enabled = enabled;
        return this;
    }

    public DefaultUser loginName(String loginName) {
        this.loginName = loginName;
        return this;
    }

    public DefaultUser password(String password) {
        this.password = password;
        return this;
    }

    public DefaultUser resources(Set<String> resources) {
        this.resources = resources;
        return this;
    }

    public DefaultUser roles(Set<String> roles) {
        this.roles = roles;
        return this;
    }
    public DefaultUser userName(String userName) {
        this.userName = userName;
        return this;
    }


    @Override
    public <T> T getOther(String key) {
        return other.containsKey(key) ? (T) other.get(key) : null;
    }

    @Override
    public DefaultUser putOther(String key, Object value) {
        other.put(key, value);
        return this;
    }

    @Override
    public Map<String, Object> getOthers() {
        return other;
    }
}
